package com.sifang.bussiness.merchant.contorller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sifang.bussiness.base.BaseController;
import com.sifang.bussiness.merchant.bean.MerchantReqPay;
import com.sifang.bussiness.merchant.bean.MerchantRspErrorResult;
import com.sifang.bussiness.merchant.bean.MerchantRspPay;
import com.sifang.bussiness.merchant.service.MerchantPayService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 支付接口
 * @author 四方支付
 *
 */
@RestController
public class MerchantPayController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(MerchantPayController.class);

	@Autowired
	MerchantPayService merchantPayService;

	@RequestMapping(value = "/pay/payReq")
	public void pay(HttpServletRequest request,HttpServletResponse response) {
		response.setHeader("Content-Type", "text/json;charset=UTF-8");
		try {
			MerchantReqPay merchantReqPay = (MerchantReqPay)request.getAttribute("msg");

			MerchantRspPay merchantRspPay = merchantPayService.pay(merchantReqPay);

			if(merchantRspPay.getCode().equals("success")) {
				writeToJson(response,merchantRspPay);
			}else {
				MerchantRspErrorResult merchantRspErrorResult = new MerchantRspErrorResult();
				merchantRspErrorResult.setCode("error");
				merchantRspErrorResult.setMsg(merchantRspPay.getMsg());
				writeToJson(response,merchantRspErrorResult);
			}
		}catch (Exception e) {
			log.error("支付异常",e);
			MerchantRspErrorResult merchantRspErrorResult = new MerchantRspErrorResult();
			merchantRspErrorResult.setCode("error");
			merchantRspErrorResult.setMsg("支付异常");
			writeToJson(response,merchantRspErrorResult);
		}
	}
}
